/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeymanlib.test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.ekeymanlib.dto.AppDeviceGrid;
import com.ekeymanlib.dto.SearchFilter;
import com.ekeymanlib.ws.EkeymanAdminWS;

public class SearchFilterUT {
	
	private static final String[] SPRING_CONFIG_FILES = new String[]{"applicationContext-lib.xml"};
	ClassPathXmlApplicationContext ctx;
	private EkeymanAdminWS ekeymanAdminWS;
	
	
	@Parameters({"searchField1","searchOp1","searchData1","searchField2","searchOp2","searchData2","searchSortIndex","searchSortOrder","searchFirstResult","searchMaxResults"}) 
	@Test
	public void testSearch(String searchField1,
			String searchOp1,
			String searchData1,
			String searchField2,
			String searchOp2,
			String searchData2,
			String searchSortIndex,
			String searchSortOrder,
			String searchFirstResult,
			String searchMaxResults) {
		
		System.out.println("Entered testSearch...");
		
		List<SearchFilter> searchFilters = new ArrayList<SearchFilter>();
		
		if(searchField1 != null && !searchField1.equalsIgnoreCase("")){
			SearchFilter sf = new SearchFilter();
			sf.setField(searchField1);
			sf.setOp(searchOp1);
			sf.setData(searchData1);
			searchFilters.add(sf);
		}

		if(searchField2 != null && !searchField2.equalsIgnoreCase("")){
			SearchFilter sf = new SearchFilter();
			sf.setField(searchField2);
			sf.setOp(searchOp2);
			sf.setData(searchData2);
			searchFilters.add(sf);
		}
		
		long count = getEkeymanAdminWS().getAppDeviceCount(searchFilters);
		System.out.println("count returned: " + count);
		
		List<AppDeviceGrid> appdevices = getEkeymanAdminWS().findAppDevices(searchFilters, 
				searchSortIndex, searchSortOrder, Integer.parseInt(searchFirstResult), Integer.parseInt(searchMaxResults));
		System.out.println("appdevices.size() returned: " + appdevices.size());
		
	    assert appdevices.size()==(int)count : "Expected appdevices.size()==count " + 
		" appdevices.size(): " + appdevices.size() + " count: " + count;
		
		System.out.println("Exited testSearch...");
	}
	
	
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Before suite...");
		
		ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
		setEkeymanAdminWS((EkeymanAdminWS)ctx.getBeanFactory().getBean("wsEkeymanAdmin"));
	}
	
	@AfterSuite
	public void afterSuite() {
		System.out.println("After suite...");
	}
	
	public void setEkeymanAdminWS(EkeymanAdminWS ekeymanAdminWS) {
		this.ekeymanAdminWS = ekeymanAdminWS;
	}
	public EkeymanAdminWS getEkeymanAdminWS() {
		return ekeymanAdminWS;
	}

}
